//= require <configurable>
Policies_Analysis_Window = Class.create(Configurable, {
    body_node_ : "",
    elemet_url_ : "",
    pol_overlay_ : null,
    initialize: function(options) 
    {
        this.setOptions(options);
        $$('body').each(function(body){
            this.body_node_ = body;
        }.bind(this));
        $(this.options.analysis_id).observe('click', this.showWindow.bindAsEventListener(this));
    },
    showWindow : function(event)
    {
        var element = Event.findElement(event, 'a.'+this.options.clickable_class);
        if(element == null || element == document)
            return false;
        event.stop();
        this.createWindow();
        if(this.pol_overlay_ == null)
            this.pol_overlay_ = new Policies_Overlay_Link_Focus(element, 'analysis-window-close', 'analysis-window-focus', this);
        this.element_url_ = element.href;
        new Ajax.Request(element.href, {
            method: "get",
            parameters : {'amode':'ajax'},
            onSuccess: this.updateInformation.bind(this),
            onFailure: function(){window.location.href = element.href}.bind(element)
        });
    },
    updateInformation : function(response)
    {
        try{
            $('analysis-window').setStyle({'textAlign':'left'}).update(response.responseText);
            response.responseText.eval_js();
        }
        catch(err)
        {
            window.location.href = this.element_url_;
        }       
    },
    createWindow : function()
    {
        if($('analysis-window-container')==null)
        {
            var width, height, left, top, font_size;
            font_size = 16;
            width = Math.floor(this.body_node_.getStyle('width').sub('px', '')*9/10); 
            height = Math.floor(document.viewport.getHeight()*9/10);
            left = Math.floor((this.body_node_.getStyle('width').sub('px', '')-width-font_size)/2) + 'px';
            top = Math.floor((document.viewport.getHeight()-height-font_size)/2) + 'px';
            width += 'px';
            height += 'px';
            //this.body_node_.insert('<div id="analysis-window-overlay"></div>');
            this.body_node_.insert('<div id="analysis-window-container" aria-hidden="true"></div>');
            $('analysis-window-container').setStyle({'left': left, 'top' : top, 'width' : width, 'height' : height}).insert('<a id="analysis-window-focus" name="analysis-window-focus" tabindex="-1"></a><a href="#" id="analysis-window-close" role="button" aria-controls="analysis-window-container">Close</a><div id="analysis-window"></div>');
            $('analysis-window-close').observe('click', this.closeWindow.bindAsEventListener(this));
        }
        this.body_node_.addClassName('analysis-window-js-overlay');
        $('analysis-window').setStyle({'textAlign':'center'}).update('<img src="output/images/ajax-loader_big.gif" alt="Loading..." class="loadingimage" />');
        //$('analysis-window-overlay').show();
        $('analysis-window-container').show().writeAttribute({'aria-hidden': 'false'});
    },
    closeWindow : function(event)
    {
        $('analysis-window-container').hide().writeAttribute({'aria-hidden': 'true'});
        //$('analysis-window-overlay').hide();
        this.body_node_.removeClassName('analysis-window-js-overlay');
        this.pol_overlay_.closeOverlay();
        this.pol_overlay_ = null;
        event.stop();
    }
});

Policies_Analysis_Window.DEFAULT_OPTIONS = {
    analysis_id: 'analysis',
    clickable_class: 'analysiswindow'
};
